package com.ruoyi.service.impl;

import com.ruoyi.domain.Ability_UserInfor;
import com.ruoyi.mapper.Ability_UserInforMapper;
import com.ruoyi.service.Ability_UserService;
import com.ruoyi.service.Consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@Service("Ability_UserService")
public class Ability_UserServiceImpl implements Ability_UserService {
    @Autowired
    Ability_UserInforMapper userInforMapper;
@Autowired
Consumer consumer;
     @Override
    public int insertUserInfor(Ability_UserInfor userInfor) {


         if(userInfor.getAccount()!=null&&userInfor.getPassword()!=null){
             try {
                 userInfor.setCname(consumer.getConsumer(userInfor.getAccount()));
                 userInfor.setKey(consumer.createAPIKey(userInfor.getCname()));
             }catch (HttpClientErrorException e){
                 throw  e;
             }catch (Exception e){
                 return -200;
             }

         }
         else{
             return 0;
         }

         try {
             userInforMapper.insertUserInfor(userInfor);
         }catch (Exception e){
             this.backupUserInfor(userInfor);
             e.printStackTrace();
             System.out.println(e.getClass());
             if(e instanceof DataIntegrityViolationException){
                 System.out.println("机构不存在");
                 return -100;
             }else {
                 return -200;
             }
         }
        return 0;
    }
     @Override
    public int updateUserInfor(Ability_UserInfor userInfor){
         if(userInfor.getId()!=null||userInfor.getAccount()!=null){
             return userInforMapper.updateUserInfor(userInfor);
         }
         else
             return 0;
    }
     @Override
    public int deleteUserInfor(Ability_UserInfor userInfor){
         if(userInfor.getId()!=null||userInfor.getAccount()!=null){
             int result = 0;
             try {
                 result = userInforMapper.deleteUserInfor(userInfor);
             }
             catch (Exception e){
                 e.printStackTrace();
                 return result;
             }
             return result;
         }
         else
             return 0;

    }
     @Override
    public int deleteUserInforById(Integer Id){
         int result = 0;
         try {
             result = userInforMapper.deleteUserInforById(Id);
         }
         catch (Exception e){
             e.printStackTrace();
             return result;
         }
         return result;
     }
     @Override
    public List<Ability_UserInfor>  selectUserInforAll(Ability_UserInfor userInfor){
         return userInforMapper.selectUserInforAll(userInfor);
    }
     @Override
    public Ability_UserInfor  selectUserInforById(Integer Id){
         return userInforMapper.selectUserInforById(Id);
    }
     @Override
    public Ability_UserInfor selectUserInforByUAccount(String account){
         return userInforMapper.selectUserInforByUAccount(account);
    }

    @Override
    public void backupUserInfor(Ability_UserInfor userInfor) {
        consumer.DelUser(userInfor.getAccount());
    }
}
